Ext.define('DragonApp.view.property.WeaponGrid', {
    extend: 'Ext.grid.Panel',

    alias: ['widget.propertyweapon'],
    plugins: 'gridfilters',
    requires: [
        'DragonApp.store.Weapon',
        'Ext.grid.filters.Filters',
        'Ext.overrides.locale.zh_CN.Filters'
    ],

    title: '装备',

    height: 600,

    margin: '0 10 0 0',
    style: {
        borderColor: '#cfcfcf',
        borderStyle: 'solid',
        border: 1
    },

    store: {
        type: 'weapon',
        storeId: 'weaponStore'
    },
    columns: [{
            xtype: 'rownumberer',
            width: 70,
            text: '序号',
            align: 'center'
        },
        {
            dataIndex: 'id',
            text: '装备ID',
            hideable: false,
            filter: {
                type: 'number',
                itemDefaults: {
                    emptyText: '请输入装备ID...'
                }
            },
            align: 'center'
        },
        {
            dataIndex: 'name',
            text: '装备名称',
            align: 'center',
            flex: 1,
            hideable: false,
            filter: {
                type: 'string',
                itemDefaults: {
                    emptyText: '请输入装备名称...'
                }
            }
        },
        {
            dataIndex: 'career',
            text: '职业',
            align: 'center',
            hideable: false,
            filter: {
                type: 'list',
                options: ['死亡骑士', '精灵游侠', '大魔法师', '幻影刺客']
            }
        },
        {
            dataIndex: 'position',
            text: '部位',
            width: 80,
            align: 'center',
            hideable: false,
            filter: {
                type: 'list',
                options: ['武器', '衣服', '鞋子', '饰品']
            }
        },
        {
            dataIndex: 'use_lv',
            text: '等级',
            width: 80,
            align: 'center',
            hideable: false,
            filter: {
                type: 'number',
                itemDefaults: {
                    emptyText: '请输入等级...'
                }
            }
        },
        {
            xtype: 'widgetcolumn',
            width: 120,
            sortable: false,
            hideable: false,
            align: 'center',
            text: '操作',
            menuDisabled: true,
            widget: {
                xtype: 'button',
                text: '添加',
                width: 80,
                iconCls: "x-fa fa-plus-circle",
                tooltip: '选取装备',
                handler: 'onAdd'
            },
            itemId: 'add'
        }
    ],
    listeners: {
        addrecord: function(record) {

            var formpanel = Ext.create('Ext.form.Panel', {
                width: 320,
                margin: 15,
                centered: true,
                record: record,
                viewModel: {
                    data: {
                        property: record
                    }
                },
                defaults: {
                    xtype: 'textfield',
                    anchor: '90%',
                    labelWidth: 70,
                    allowBlank: false,
                    msgTarget: 'side',
                    labelAlign: "left",
                    enableKeyEvents: true,
                    listeners: {
                        specialKey: 'onTextFieldSpecialKey'
                    }
                },
                items: [{
                    fieldLabel: '装备ID',
                    editable: false,
                    name: 'id',
                    bind: '{property.id}'
                }, {
                    fieldLabel: '装备名称',
                    editable: false,
                    name: 'name',
                    bind: '{property.name}'
                }, {
                    fieldLabel: '装备数量',
                    xtype: 'numberfield',
                    editable: true,
                    minValue: 1,
                    value: 1,
                    name: 'num'
                }]
            });

            var win = Ext.create('Ext.window.Window', {
                title: '添加装备',
                modal: true,
                width: 350,
                height: 200,
                resizable: false,
                closeAction: 'destroy',
                items: [formpanel],
                buttons: [{
                    text: '取消',
                    handler: function(button) {
                        button.up('window').down('form').destroy();
                        button.up('window').destroy();
                    }
                }, {
                    text: '确定',
                    handler: function(button) {
                        var window = button.up('window'),
                            form = window.down('form'),
                            values = form.getValues();

                        var new_record = Ext.create('DragonApp.model.SelectedProp', {
                            id: record.get('id'),
                            name: record.get('name'),
                            num: values.num
                        });

                        //增加记录
                        var selectedPropStore = Ext.data.StoreManager.lookup('selected_prop');
                        selectedPropStore.add(new_record);

                        //销毁窗体
                        form.destroy();
                        window.destroy();
                    }
                }]
            }).show();
        }
    }
});